package com.alibaba.citrus.cr.unified.inventory.facade.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.CompanySDO;
import com.epoch.app.bcorder.service.BaseDataService;
import com.epoch.app.crplatformenhance.model.dto.ESPageQueryCustomerByCustomRequest;
import com.epoch.app.crplatformenhance.model.dto.ESPageQueryCustomerByCustomResponse;
import com.epoch.app.crplatformenhance.model.dto.ESPageQueryCustomerRequest;
import com.epoch.app.crplatformenhance.model.dto.ESPageQueryCustomerResponse;
import com.epoch.app.crplatformenhance.model.dto.PageQueryAddressListForFactoryRequest;
import com.epoch.app.crplatformenhance.model.dto.PageQueryAddressListForFactoryResponse;
import com.epoch.app.crplatformenhance.opecustomerindex.model.OpeCustomerIndex;
import com.epoch.app.crplatformenhance.service.CrPlatformEnhanceService;
import com.epoch.app.crunifiedinventory.api.allocateapplyorderdropdownselection.dto.QueryBranchCustomersRequest;
import com.epoch.app.crunifiedinventory.api.allocateapplyorderdropdownselection.service.AllocateApplyOrderDropDownSelectionService;
import com.epoch.app.crunifiedinventory.dto.request.ReasonForRejectionDTO;
import com.epoch.app.crunifiedinventory.dto.response.DocumentReasonDTO;
import com.epoch.app.crunifiedinventory.dto.response.DocumentTypeDTO;
import com.epoch.app.crunifiedinventory.model.dto.AllocateApplyOrderStatusDTO;
import com.epoch.app.crunifiedinventory.model.dto.BranchCustomersDTO;
import com.epoch.app.crunifiedinventory.model.dto.QueryCustomerAddressDTO;
import com.epoch.app.crunifiedinventory.model.dto.QueryCustomerAddressRequest;
import com.epoch.app.crunifiedinventory.model.dto.QueryMonthToSubdivideEnumDTO;
import com.epoch.app.crunifiedinventory.model.enums.MonthToSubdivideEnum;
import com.epoch.app.crunifiedinventory.sdo.enums.AllocateApplyOrderStatusEnum;
import com.epoch.app.crunifiedinventory.sdo.enums.DocumentReasonEnum;
import com.epoch.app.crunifiedinventory.sdo.enums.DocumentTypeEnum;
import com.epoch.app.crunifiedinventory.sdo.enums.ReasonForRejectionEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Primary
public class AllocateApplyOrderDropDownSelectionServiceImpl implements AllocateApplyOrderDropDownSelectionService {

    private final static String ADDRESS_CODE = "addressCode";
    private final static String ADDRESS_NAME = "addressName";
    private final static String CUSTOMER_CODE = "customerCode";
    private final static String CUSTOMER_NAME = "customerName";
    private final static String ID = "id";
    private final static String ADDRESS = "address";
    @Resource
    private CrPlatformEnhanceService otsPlatformEnhanceService;

    @Resource
    private BaseDataService baseDataService;
    @Override
    public Result<List<DocumentTypeDTO>> queryDocumentType() {
        DocumentTypeEnum[] list = DocumentTypeEnum.values();
        List<DocumentTypeDTO> documentTypeDTOS = new ArrayList<>();
        for (DocumentTypeEnum documentTypeEnum:list){
            DocumentTypeDTO documentTypeDTO = new DocumentTypeDTO();
            documentTypeDTO.setValue(documentTypeEnum.getCode());
            documentTypeDTO.setLabel(documentTypeEnum.getDesc());
            documentTypeDTOS.add(documentTypeDTO);
        }
        return Result.success(documentTypeDTOS);
    }

    @Override
    public Result<List<DocumentReasonDTO>> queryDocumentReason() {
        DocumentReasonEnum[] list = DocumentReasonEnum.values();
        List<DocumentReasonDTO> documentReasonDTOS = new ArrayList<>();
        for (DocumentReasonEnum documentReasonEnum:list){
            DocumentReasonDTO documentReasonDTO = new DocumentReasonDTO();
            documentReasonDTO.setValue(documentReasonEnum.getCode());
            documentReasonDTO.setLabel(documentReasonEnum.getDesc());
            documentReasonDTOS.add(documentReasonDTO);
        }
        return Result.success(documentReasonDTOS);
    }

    @Override
    public Result<List<ReasonForRejectionDTO>> queryReasonForRejection() {
        ReasonForRejectionEnum[] list = ReasonForRejectionEnum.values();
        List<ReasonForRejectionDTO> reasonForRejectionDTOS = new ArrayList<>();
        for (ReasonForRejectionEnum reasonForRejectionEnum:list){
            ReasonForRejectionDTO reasonForRejectionDTO = new ReasonForRejectionDTO();
            reasonForRejectionDTO.setValue(reasonForRejectionEnum.getCode());
            reasonForRejectionDTO.setLabel(reasonForRejectionEnum.getDesc());
            reasonForRejectionDTOS.add(reasonForRejectionDTO);
        }
        return Result.success(reasonForRejectionDTOS);
    }
    @Override
    public Result<List<AllocateApplyOrderStatusDTO>> queryAllocateApplyOrderStatus(){
        AllocateApplyOrderStatusEnum[] list = AllocateApplyOrderStatusEnum.values();
        List<AllocateApplyOrderStatusDTO> allocateApplyOrderStatusDTOS = new ArrayList<>();
        for (AllocateApplyOrderStatusEnum allocateApplyOrderStatusEnum:list){
            AllocateApplyOrderStatusDTO allocateApplyOrderStatusDTO = new AllocateApplyOrderStatusDTO();
            allocateApplyOrderStatusDTO.setValue(allocateApplyOrderStatusEnum.getCode());
            allocateApplyOrderStatusDTO.setLabel(allocateApplyOrderStatusEnum.getDesc());
            allocateApplyOrderStatusDTOS.add(allocateApplyOrderStatusDTO);
        }
        return Result.success(allocateApplyOrderStatusDTOS);
    }

    /**
     * 2022.02.24 更新分子公司客户的权限查询
     * @param queryBranchCustomersRequest
     * @return
     */
    @Override
    public Result<List<BranchCustomersDTO>> queryBranchCustomers(QueryBranchCustomersRequest queryBranchCustomersRequest){
        ESPageQueryCustomerByCustomRequest esPageQueryCustomerByCustomRequest = new ESPageQueryCustomerByCustomRequest();
        if (Objects.nonNull(queryBranchCustomersRequest)&& StringUtils.isNotBlank(queryBranchCustomersRequest.getName())){
            esPageQueryCustomerByCustomRequest.setName(queryBranchCustomersRequest.getName());
        }
        if (StringUtils.isNotEmpty(queryBranchCustomersRequest.getCode())){
            esPageQueryCustomerByCustomRequest.setCode(queryBranchCustomersRequest.getCode());
        }
        esPageQueryCustomerByCustomRequest.setStatus("1");
        ESPageQueryCustomerByCustomResponse esPageQueryCustomerByCustomResponse = otsPlatformEnhanceService.eSPageQueryCustomerByCustom(esPageQueryCustomerByCustomRequest);
        List<BranchCustomersDTO> list = new ArrayList<>();
        if (Objects.nonNull(esPageQueryCustomerByCustomResponse)&&Objects.nonNull(esPageQueryCustomerByCustomResponse.getResult())){
            for (OpeCustomerIndex opeCustomerIndex:esPageQueryCustomerByCustomResponse.getResult()){
                BranchCustomersDTO branchCustomersDTO = new BranchCustomersDTO();
                branchCustomersDTO.setCode(opeCustomerIndex.getCode());
                branchCustomersDTO.setName(opeCustomerIndex.getName());
                list.add(branchCustomersDTO);
            }
        }
        return Result.success(list);
//        ESPageQueryCustomerRequest esPageQueryCustomerRequest = new ESPageQueryCustomerRequest();
//        if (Objects.nonNull(queryBranchCustomersRequest)&& StringUtils.isNotBlank(queryBranchCustomersRequest.getName())){
//            esPageQueryCustomerRequest.setName(queryBranchCustomersRequest.getName());
//        }
//        if (StringUtils.isNotEmpty(queryBranchCustomersRequest.getCode())){
//            esPageQueryCustomerRequest.setCode(queryBranchCustomersRequest.getCode());
//        }
//        esPageQueryCustomerRequest.setStatus("1");
//        ESPageQueryCustomerResponse esPageQueryCustomerResponse = otsPlatformEnhanceService.eSPageQueryCustomer(esPageQueryCustomerRequest);
//        List<BranchCustomersDTO> list = new ArrayList<>();
//        if (Objects.nonNull(esPageQueryCustomerResponse)&&Objects.nonNull(esPageQueryCustomerResponse.getResult())){
//            for (OpeCustomerIndex opeCustomerIndex:esPageQueryCustomerResponse.getResult()){
//                BranchCustomersDTO branchCustomersDTO = new BranchCustomersDTO();
//                branchCustomersDTO.setCode(opeCustomerIndex.getCode());
//                branchCustomersDTO.setName(opeCustomerIndex.getName());
//                list.add(branchCustomersDTO);
//            }
//        }
//        return Result.success(list);
    }

    @Override
    public Result<List<QueryCustomerAddressDTO>> queryCustomerAddress(QueryCustomerAddressRequest queryCustomerAddressRequest){
        Result<CompanySDO> companySDOResult = baseDataService.getLoginEmployeeCompany();
        String code = null;
        if(Objects.nonNull(companySDOResult)&& Objects.nonNull(companySDOResult.getResult())) {
            code = companySDOResult.getResult().getCode();
        }
        PageQueryAddressListForFactoryRequest pageQueryAddressListRequest = new PageQueryAddressListForFactoryRequest();
        if (Objects.nonNull(queryCustomerAddressRequest)){
            if (StringUtils.isNotBlank(queryCustomerAddressRequest.getName())){
                //2022.11.02 工厂直发预报单创建时 收货地址 可以根据地址名称和地址编码模糊
                pageQueryAddressListRequest.setAddressCodeNameLike(queryCustomerAddressRequest.getName());
//                pageQueryAddressListRequest.setAddressName(queryCustomerAddressRequest.getName());
            }
            if (StringUtils.isNotBlank(queryCustomerAddressRequest.getCode())){
                pageQueryAddressListRequest.setAddressCode(queryCustomerAddressRequest.getCode());
            }
            if (StringUtils.isNotBlank(queryCustomerAddressRequest.getAddressName())){
                pageQueryAddressListRequest.setAddressName(queryCustomerAddressRequest.getAddressName());
            }
            if (Objects.nonNull(queryCustomerAddressRequest.getStart())){
                pageQueryAddressListRequest.setStart(queryCustomerAddressRequest.getStart());
            }
            if (Objects.nonNull(queryCustomerAddressRequest.getLimit())){
                pageQueryAddressListRequest.setLimit(queryCustomerAddressRequest.getLimit());
            }
            if (Objects.nonNull(queryCustomerAddressRequest.getCustomerName())){
                //2023.04.12 工厂直发预报单创建时 客户名称 可以根据客户名称和客户编码模糊
                pageQueryAddressListRequest.setCustomerCodeNameLike(queryCustomerAddressRequest.getCustomerName());
//                pageQueryAddressListRequest.setCustomerName(queryCustomerAddressRequest.getCustomerName());
            }
        }
        if (StringUtils.isNotEmpty(queryCustomerAddressRequest.getId())){
            pageQueryAddressListRequest.setId(queryCustomerAddressRequest.getId());
        }

        //set组织内部id过滤出总部客户地址
        if (StringUtils.isNotEmpty(code)) {
            pageQueryAddressListRequest.setCustomerCode(code);
        }
        PageQueryAddressListForFactoryResponse pageQueryAddressListResponse = otsPlatformEnhanceService.pageQueryAddressListForFactory(pageQueryAddressListRequest);
        List<QueryCustomerAddressDTO> list = new ArrayList<>();
        if (Objects.nonNull(pageQueryAddressListResponse)&&Objects.nonNull(pageQueryAddressListResponse.getResult())){
            for (Object o:pageQueryAddressListResponse.getResult()){
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(o));
                QueryCustomerAddressDTO queryCustomerAddressDTO = new QueryCustomerAddressDTO();
                queryCustomerAddressDTO.setCode(jsonObject.getString(ADDRESS_CODE));
                queryCustomerAddressDTO.setName(jsonObject.getString(ADDRESS_NAME));
                queryCustomerAddressDTO.setCustomerCode(jsonObject.getString(CUSTOMER_CODE));
                queryCustomerAddressDTO.setCustomerName(jsonObject.getString(CUSTOMER_NAME));

                queryCustomerAddressDTO.setId(jsonObject.getString(ID));
                queryCustomerAddressDTO.setAddressCode(jsonObject.getString(ADDRESS_CODE));
                queryCustomerAddressDTO.setAddress(jsonObject.getString(ADDRESS));
                queryCustomerAddressDTO.setAddressName(jsonObject.getString(ADDRESS_NAME));
                list.add(queryCustomerAddressDTO);
            }
        }
        Result<List<QueryCustomerAddressDTO>> listResult = new Result<>();
        listResult.setTotal(Objects.isNull(pageQueryAddressListResponse.getTotal())?0:pageQueryAddressListResponse.getTotal());
        listResult.setSuccess(true);
        listResult.setResult(list);
        return listResult;
    }

    @Override
    public Result<List<QueryMonthToSubdivideEnumDTO>> queryMonthToSubdivideEnum(){
        MonthToSubdivideEnum[] list = MonthToSubdivideEnum.values();
        List<QueryMonthToSubdivideEnumDTO> queryMonthToSubdivideEnumDTOS = new ArrayList<>();
        for (MonthToSubdivideEnum monthToSubdivideEnum:list){
            QueryMonthToSubdivideEnumDTO queryMonthToSubdivideEnumDTO = new QueryMonthToSubdivideEnumDTO();
            queryMonthToSubdivideEnumDTO.setCode(monthToSubdivideEnum.getValue());
            queryMonthToSubdivideEnumDTO.setName(monthToSubdivideEnum.getLabel());
            queryMonthToSubdivideEnumDTOS.add(queryMonthToSubdivideEnumDTO);
        }
        return Result.success(queryMonthToSubdivideEnumDTOS);
    }
}
